package com.song.oscsong.ui;


import java.util.List;

import org.apache.http.Header;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnTouchModeChangeListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.song.oscsong.R;
import com.song.oscsong.api.OSCChinaApi;
import com.song.oscsong.bean.News;
import com.song.oscsong.bean.NewsList;
import com.song.oscsong.utils.XmlUtils;
import com.song.oscsong.viewpagerfragment.ExploreFragment;
import com.song.oscsong.viewpagerfragment.MyInformationFragment;
import com.song.oscsong.viewpagerfragment.NewsViewPagerFragment;
import com.song.oscsong.viewpagerfragment.TweetsViewPagerFragment;
import com.song.oscsong.widget.MyFragmentTabHost;

public class MainActivity extends FragmentActivity implements OnTouchListener,OnTabChangeListener,OnClickListener {
	
//	private  MyFragmentTabHost  mTabHost;
	
	private FragmentTabHost mTabHost;
	
	private int current;
	
	private ActionBar mActionBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		mTabHost = (MyFragmentTabHost)findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		mActionBar = getActionBar();
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		mActionBar.setDisplayShowTitleEnabled(true);
		mActionBar.setTitle("开源中国");
//		mTabHost.getTabWidget().setDividerDrawable(resId);
		initView();
		
		
	}

	private void initView() {
		
        if (android.os.Build.VERSION.SDK_INT > 10) {
            mTabHost.getTabWidget().setShowDividers(0);
        }
		
		initTabs();
		mTabHost.setCurrentTab(0);
		mTabHost.setOnTabChangedListener(this);
	}

	private void initTabs() {
		
		String[]  tags = {"zh","dt","ks","fi","me"};
		String[] titles = {"综合","动弹","快速","发现","我"};
		int[] res = {R.drawable.tab_icon_new , R.drawable.tab_icon_tweet ,R.drawable.tab_icon_new  ,R.drawable.tab_icon_explore ,R.drawable.tab_icon_me};
		Class[] clazzs = {NewsViewPagerFragment.class,TweetsViewPagerFragment.class, null
						  ,ExploreFragment.class,MyInformationFragment.class};
		int count = tags.length;
		
		for(int i = 0;i<count ;i++)
		{
			TabSpec space = mTabHost.newTabSpec(tags[i]);
			View indicator = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_indicator, null);
			TextView title = (TextView) indicator.findViewById(R.id.tab_title);
			title.setText(titles[i]);
			Drawable drawable = this.getResources().getDrawable(res[i]);
			title.setCompoundDrawablesWithIntrinsicBounds(null,drawable ,null,null);
			if(i == 2)
			{
				indicator.setVisibility(View.INVISIBLE);
//				mTabHost.setNO
			}
			
			space.setContent(new TabContentFactory(){

				@Override
				public View createTabContent(String tag) {
					// TODO Auto-generated method stub
					return new View(MainActivity.this);
				}});
			space.setIndicator(indicator);
			mTabHost.addTab(space ,clazzs[i] , null);
			mTabHost.getTabWidget().getChildAt(i).setOnClickListener(new TabClick(i));
		}

	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String tag = v.getTag().toString();
		mTabHost.setCurrentTabByTag(tag);
	}
	
	@Override
	public void onTabChanged(String tabId) {
		
		final int count = mTabHost.getTabWidget().getChildCount();
		Log.i("song", "click zhe tab");
		for(int i = 0 ;i< count ;i++)
		{
			View  v= mTabHost.getTabWidget().getChildAt(i);
			if(i == mTabHost.getCurrentTab())
			{
				v.setSelected(true);
			}else{
				v.setSelected(false);
			}
			
			
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
		super.onTouchEvent(event);
		boolean flag = false;
		if(event.getAction() == MotionEvent.ACTION_DOWN  && v.equals(mTabHost.getCurrentTabView()))
		{
			android.support.v4.app.Fragment currentFrgament = getCurrentFragment();
			
			
			
		}
		
		
		return true;
	}

	private android.support.v4.app.Fragment getCurrentFragment() {
		
		return getSupportFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
	}



	class TabClick implements OnClickListener
	{
		
		private int  tag;
		
		public TabClick(int tag) {
			super();
			this.tag = tag;
		}

		@Override
		public void onClick(View v) {
			
			mTabHost.setCurrentTab(tag);
		}
		
	}

}
